/* Serial Servo Control
 *
 * Derived from:
 *   Sweep by BARRAGAN <http://barraganstudio.com> 
 */

#include <Servo.h>
#include <stdlib.h> //atoi
#include <avr/sleep.h>

#define LED_PIN 13
#define SERVO_PIN 11

Servo myservo;       // create servo object to control a servo 
                     // a maximum of eight servo objects can be created 

int pos = 0;         // variable to store the servo position 
char led_state = 0;  // variable to store the state of the LED
char buffer[9];      // buffer for storing the inputs from the Serial
                     //   interface
char buffer_pos = 0; // current writing position for the buffer


void toggleLed()
{
  // If the LED is on
  if (led_state) 
  { 
    // then turn it off.
    digitalWrite(LED_PIN,LOW);
  }
  else
  {
    // otherwise, turn it on
    digitalWrite(LED_PIN,HIGH);
  }
  
  // toggle the LED state variable
  led_state=!led_state;
}

void setup() 
{ 
  // start serial port at 9600 bps:
  Serial.begin(9600);

  pinMode(LED_PIN, OUTPUT);  // enable the LED pin
  myservo.attach(SERVO_PIN); // attaches the servo pin to the servo object 

} 
 
 
void loop() 
{ 
  int read_val;  // variable to store the integer from the Serial.read() method
                 // this is the ASCII value of what is typed in at the Serial
                 // Monitor window
                 
  // if we've got something ready from the serial interface
  if ( Serial.available() > 0 )
  {
    // read in the data, up to the max size of the buffer.
    while( -1 != (read_val = Serial.read() ) && buffer_pos < 8)
    {
      // put the value in the buffer, and increment the position
      buffer[buffer_pos++] = read_val;
    }
    buffer[buffer_pos] = '\0';       // terminate the string.
    Serial.print("Read in value: "); // add some feedback to the Serial Monitor
    Serial.println(buffer);          //   so we know the command was received
    pos = atoi(buffer);              // turn the input data into an integer value
    if (pos == -1)                   // If we've received the "all done" command
    {
      set_sleep_mode(SLEEP_MODE_PWR_DOWN);  // set the sleep mode
      sleep_enable();                // enable sleeping
      sleep_mode();                  // then activate the sleep mode
    }
    pos = (pos < 170 ? pos : 170);   // clamp the input values at reasonable 
    pos = (pos > 0 ? pos : 0);       //  values for your servo.  Mine is finicky ~ 180
    Serial.print("Moving to ");      // Tell Serial Monitor where we're actually going
    Serial.print(pos);
    myservo.write(pos);              // And go there
    toggleLed();
    delay(10000); 
    buffer_pos = 0;                  // And clean up after ourselves.
    memset(buffer,0,9);
  }
  else
  {
    
    Serial.println("Moving 0 to 180");
    for(pos = 0; pos < 180; pos += 5)  // goes from 0 degrees to 180 degrees 
    {                                  // in steps of 5 degrees
      myservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(50);                       // waits 50ms for the servo to reach the position 
      toggleLed();
    } 
    Serial.println("Moving 180 to 0");
    for(pos = 180; pos>=0; pos-=5)     // goes from 180 degrees to 0 degrees 
    {                                
      myservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(50);                       // waits 50ms for the servo to reach the position 
      toggleLed();
    } 
  }
} 
